<?php

namespace app\beautiful\controller\admin\mall;

use app\BaseController;

use app\beautiful\controller\admin\setting\Send;
use app\beautiful\model\mall\FreightTemplateModel;

use app\beautiful\model\mall\FreightTemplatePieceModel;
use app\beautiful\model\setting\SendCityModel;
use app\beautiful\model\setting\SendSelfModel;
use app\common\model\ShopModel;


class FreightTemplate extends BaseController{

    protected $name='运费模板';



    public function show()

    {

        if(null === $freight_template = FreightTemplateModel::where('shop_id',$this->request->shopId)->find()){

            $freight_template = new FreightTemplateModel();

            $freight_template->shop_id = $this->request->shopId;

            $freight_template->no_delivery_area = [];

            $freight_template->save();

        }



        if($freight_template->freight_type==2){

            if($freight_template->valuation_type==1){

                if(null === $freight_template_piece = FreightTemplatePieceModel::where('freight_template_id',$freight_template->freight_template_id)->find()){

                    $freight_template_piece = new FreightTemplatePieceModel();

                    $freight_template_piece->shop_id = $this->request->shopId;

                    $freight_template_piece->freight_template_id = $freight_template->freight_template_id;

                    $freight_template_piece->piece_data = [];

                    $freight_template_piece->save();

                }

            }

        }



        $piece=[

            'base_piece_num'=>isset($freight_template_piece) ? $freight_template_piece->base_piece_num : '',

            'base_piece_price'=>isset($freight_template_piece) ? $freight_template_piece->base_piece_price : '',

            'add_piece_num'=>isset($freight_template_piece) ? $freight_template_piece->add_piece_num : '',

            'add_piece_price'=>isset($freight_template_piece) ? $freight_template_piece->add_piece_price : '',

            'piece_data'=>isset($freight_template_piece) ? $freight_template_piece->piece_data : [],

        ];



        $detail=[

            'name'=>$freight_template->name,

            'hours'=>$freight_template->hours,

            'freight_type'=>$freight_template->freight_type,

            'valuation_type'=>$freight_template->valuation_type,

            'no_delivery_area'=>$freight_template->no_delivery_area??[],

            'is_open'=>$freight_template->is_open,

            'is_default'=>$freight_template->is_default,

        ];



        $return=[

            'detail'=>$detail,

            'piece'=>$piece

        ];



        return $this->success($return);

    }



    public function save()

    {

        $name=$this->request->param('name');

        $hours=$this->request->param('hours/d',0);

        $freight_type=$this->request->param('freight_type/d',1);

        $valuation_type=$this->request->param('valuation_type/d',1);

        $no_delivery_area=$this->request->param('no_delivery_area/a',[]);

        $is_default=$this->request->param('is_default/d',0);

        $is_open=$this->request->param('is_open/d',0);

        $from_type=$this->request->param('from_type/d',1);


        $areas=[];

        if(empty($name)) return $this->error('请输入名称');

        if(empty($hours) || $hours<=0) return $this->error('请选择发货时间');

        if(empty($freight_type) || !in_array($freight_type,[1,2,3])) return $this->error('请选择邮费设置');

        if(empty($valuation_type) || !in_array($valuation_type,[1])) $valuation_type=1;

        if(!empty($no_delivery_area)){

            foreach ($no_delivery_area as $v){

                if(in_array($v,$areas)) return $this->error('暂不发货地区的'.$v.'已经被其他选中，您无法选择');

                if(!in_array($v,$areas)) $areas[]=$v;

            }

        }


        //按件

        if($freight_type==2 && $valuation_type==1){

            $base_piece_num=$this->request->param('base_piece_num/d',0);

            $base_piece_price=$this->request->param('base_piece_price/f',0);

            $add_piece_num=$this->request->param('add_piece_num/d',0);

            $add_piece_price=$this->request->param('add_piece_price/f',0);

            $piece_data=$this->request->param('piece_data/a',[]);



            if(empty($base_piece_num) || $base_piece_num<=0) return $this->error('请输入基础件数');

            if(empty($base_piece_price) || $base_piece_price<=0) return $this->error('请输入基础价格');

            if(empty($add_piece_num) || $add_piece_num<=0) return $this->error('请输入附加件数');

            if(empty($add_piece_price) || $add_piece_price<=0) return $this->error('请输入附加价格');

            if(!empty($piece_data)){
                
                foreach ($piece_data as $k => $v){

                    if(empty($v['designated_area']) || !is_array($v['designated_area'])) return $this->error('请选择第'.($k+1).'行的指定区域');

                    foreach ($v['designated_area'] as $val){

                        if(in_array($val,$areas)) return $this->error('第'.($k+1).'行发货地区的'.$val.'已经被其他选中，您无法选择');

                        if(!in_array($val,$areas)) $areas[]=$val;

                    }

                    if(empty($v['base_piece_num']) || $v['base_piece_num']<=0) return $this->error('请输入第'.($k+1).'行的基础件数，且不能少于0');

                    if(empty($v['base_piece_price']) || $v['base_piece_price']<=0) return $this->error('请输入第'.($k+1).'行的基础价格，且不能少于0');

                    if(empty($v['add_piece_num']) || $v['add_piece_num']<=0) return $this->error('请输入第'.($k+1).'行的附加件数，且不能少于0');

                    if(empty($v['add_piece_price']) || $v['add_piece_price']<=0) return $this->error('请输入第'.($k+1).'行的附加价格，且不能少于0');

                }

            }

        }



        //操作模板

        if(null === $freight_template = FreightTemplateModel::where('shop_id',$this->request->shopId)->find()){

            $freight_template = new FreightTemplateModel();

            $freight_template->shop_id = $this->request->shopId;

        }

        if($from_type==2){
            if($is_default>0){
                SendCityModel::where(['shop_id'=>$this->request->shopId,'is_open'=>1,'is_default'=>1])->save(['is_default'=>0]);
                SendSelfModel::where(['shop_id'=>$this->request->shopId,'is_open'=>1,'is_default'=>1])->save(['is_default'=>0]);
            }else{
                $send_city = SendCityModel::where(['shop_id'=>$this->request->shopId,'is_open'=>1,'is_default'=>1])->find();
                $send_self = SendSelfModel::where(['shop_id'=>$this->request->shopId,'is_open'=>1,'is_default'=>1])->find();
                if(empty($send_city) && empty($send_self)) return $this->error('必须开启一种默认配送方式');
            }


        }else{
            if($is_default>0) {

                if ($is_open < 1) return $this->error('默认配送方式不能关闭');

                $send = new Send($this->app);

                $default_send_type = $send->defaultSendType();

                if($default_send_type!==1) $send->updateDefaultSendType($default_send_type,['is_default'=>0]);

                ShopModel::where([['shop_id','=',$this->request->shopId]])->update(['default_send_type'=>1]);
            }else{

                if($freight_template->is_default>0) return $this->error('必须开启一种默认配送方式');
            }

        }

        $freight_template->name = $name;

        $freight_template->hours = $hours;

        $freight_template->freight_type = $freight_type;

        $freight_template->valuation_type = $valuation_type;

        $freight_template->no_delivery_area = $no_delivery_area;

        $freight_template->is_open = $is_open;

        $freight_template->is_default = $is_default;

        $freight_template->save();



        //操作计件

        if($freight_type==2 && $valuation_type==1){

            if(null === $freight_template_piece = FreightTemplatePieceModel::where('freight_template_id',$freight_template->freight_template_id)->find()){

                $freight_template_piece = new FreightTemplatePieceModel();

                $freight_template_piece->shop_id = $this->request->shopId;

                $freight_template_piece->freight_template_id = $freight_template->freight_template_id;

            }



            $freight_template_piece->base_piece_num = $base_piece_num;

            $freight_template_piece->base_piece_price = $base_piece_price;

            $freight_template_piece->add_piece_num = $add_piece_num;

            $freight_template_piece->add_piece_price = $add_piece_price;

            $freight_template_piece->piece_data = $piece_data;

            $freight_template_piece->save();

        }

        if($from_type==2 && $is_default>0){
            ShopModel::where(['shop_id'=>$this->request->shopId])->save(['default_send_type'=>1]);
        }



        return $this->success('设置成功');



    }

}

